const express = require('express');
const router = express.Router();
const { sendSmsCode } = require('../services/smsService');

/**
 * 发送短信验证码
 * POST /api/sms/send-code
 */
router.post('/send-code', async (req, res) => {
  try {
    const { phone } = req.body;

    // 验证手机号
    if (!phone) {
      return res.status(400).json({
        success: false,
        message: '请提供手机号'
      });
    }

    if (!/^1[3-9]\d{9}$/.test(phone)) {
      return res.status(400).json({
        success: false,
        message: '手机号格式不正确'
      });
    }

    // 发送验证码
    const result = await sendSmsCode(phone);
    
    res.json({
      success: true,
      message: result.message,
      devCode: process.env.NODE_ENV === 'development' ? result.devCode : undefined
    });
  } catch (error) {
    console.error('发送验证码失败:', error);
    res.status(400).json({
      success: false,
      message: error.message || '发送验证码失败'
    });
  }
});

module.exports = router;
